<?php
namespace Home\Controller;
use Common\Model\ConsumerModel;
use Think\Controller;
use Common\Model\ConsumerObject;
use Think\Model;

class IndexController extends Controller {
    /**
     *主页控制器
     */
    public function index(){
//        $model=D('Consumer');
//        /** @var ConsumerObject $consumer */
//        $consumer=$model->fetch(1);
//        echo $consumer->Username;
//        $shoppingcart=$consumer->fetchShoppingcart();
//        foreach($shoppingcart as $value){
//            print_r($value->Goods->Name);
//        }
//        $order=$consumer->fetchOrder();
//        foreach($order as $value){
//            print_r($value->Goods->Price);
//        }
        $this->assign('user',$_SESSION['id']);
        if($_SESSION['id']) {
            $consumer=D('Consumer');
            $consumerID=$consumer->fetchID($_SESSION['id']);
            $shoppingcart=D('shoppingcart');
            $result=$shoppingcart->where('ConsumerID='.$consumerID)->count();
            $this->assign('count',$result);
        }
        $goods=D('Goods');
        $fen = array();
        $fen[] = array();
        for($i=1;$i<=17;$i++)
            $fen[]=$goods->getFenGoods($i);
        $this->assign("fen",$fen);
        $this->display();
     }
    public function login(){
        $this->display();
    }
    public function login_action(){
        $data=D('Consumer');
        $data->queryUsername(I('post.username'));
        $result=$data->password;
        if($result==null)
            $this->error('用户不存在,请重新登录','login');
        else if($result!=$_POST['password'])
            $this->error('密码不正确,请重新登录','login');
        else
        {
            $_SESSION['id']=$_POST['username'];
            $this->success('密码正确',U('/Index/Index/',array('sdasfsaf',rand())));
        }
    }
    public function register(){
        $this->display();
    }
    public function register_action()
    {
        $data = D('Consumer');
        $data->queryUsername(I('post.username'));
        $result = $data->password;
        if ($result)
            $this->error('用户已存在,请重新注册', 'register');
        else if ($_POST['password1'] != $_POST['password2'])
            $this->error('密码不一致', 'register');
        else {
            $data1['Username'] = $_POST['username'];
            $data1['Password'] = $_POST['password1'];
            $data->addConsumer($data1);
            $this->success('注册成功','/Index/login');
        }
    }
    public function shangpinxiangqing1(){
        $this->assign('user',$_SESSION['id']);
        $goods=D('Goods');
        $data=$goods->getGoods(I('get.id'));
        $data=$data[0];
        $data['edition']=explode(',',$data['edition']);
        $data['color']=explode(',',$data['color']);
        $this->assign("data",$data);
        $this->display();
    }
    public function shangpinxiangqing1_action(){
        if($_SESSION['id']=='')
            $this->error("未登录","/Index/login");
        if(I('post.type')==null||I('post.color')==null)
            $this->error("请选择版本和颜色",'/Index/shangpinxiangqing1/id/'.$_POST['id']);
        $goods=D('Goods');
        $result=$goods->getGoods(I('post.id'));
        $result=$result[0];
        $consumer=D('Consumer');
        $consumerID=$consumer->fetchID($_SESSION['id']);
        $data['GoodsID']=I('post.id');
        $data['ConsumerID']=$consumerID;
        $data['Number']=I('post.number');
        $data['GoodsInformation']=$result['name'].','.$_POST['type'].','.$_POST['color'];
        $add=M('shoppingcart');
        if($add->add($data))
            $this->success("添加成功，继续购物",U('/Index/Index/',array('sdasfsaf',rand())));
        else
            $this->error("添加失败，重新添加",'/Index/shangpinxiangqing1/id/'.$_POST['id']);

    }
    public function shoppingcart(){
        $this->assign('user',$_SESSION['id']);
        if($_SESSION['id']) {
            $consumer=D('Consumer');
            $consumerID=$consumer->fetchID($_SESSION['id']);
            $shoppingcart=D('shoppingcart');
            $count=$shoppingcart->where('ConsumerID='.$consumerID)->count();
            $result=$shoppingcart->where('ConsumerID='.$consumerID)->select();
            $total=0.0;
            foreach($result as $key => $vo){
                $goods=D('Goods');
                $goods=$goods->where('ID='.$vo['goodsid'])->find();
                $result[$key]['price']=$goods['price'];
                $result[$key]['xiaoji']=$goods['price']*$result[$key]['number'];
                $total+=$result[$key]['xiaoji'];
            }
            $this->assign('total',$total);
            $this->assign('data',$result);
            $this->assign('count',$count);
            $this->display();
       }
    }
    public  function shoppingcart_action(){
        $shoppingcart=M('shoppingcart');
        $result=$shoppingcart->delete($_POST['id']);
        echo $result;
    }
    public  function confirm_order(){
        $goodscontent=null;
        foreach($_POST['goods'] as $vo){
            if($goodscontent)
                $goodscontent=$goodscontent.';'.$vo;
            else
                $goodscontent=$goodscontent.$vo;
        }
        $this->assign('checkedGoodsID',I("post.checkedGoodsID"));
        $this->assign('total',I("post.total"));
        $this->assign('goods',$_POST['goods']);
        $this->assign('goodscontent',$goodscontent);
        $this->display();
    }
    public  function pay_order(){
        $this->assign('user',$_SESSION['id']);
        if(I('post.name')==''||I('post.telephone')==''||I('post.zipcode')==''||I('post.address')=='')
            $this->error('信息不完整！');
        $data['Goods']=I('post.goods');
        $consumer=D('Consumer');
        $consumerID=$consumer->fetchID($_SESSION['id']);$_SESSION['id'];
        $data['ConsumerID']=$consumerID;
        $data['Total']=I('post.total');
        $data['Name']=I('post.name');
        $data['Telephone']=I('post.telephone');
        $data['ZipCode']=I('post.zipcode');
        $data['Address']=I('post.address');
        $order=M('Order');
        $order->add($data);
        $str=I('post.checkedGoodsID');
        $arrayID=explode(",",$str);
        foreach($arrayID as $vo){
            $delete=M('shoppingcart');
            $delete->delete($vo);
        }
        $this->assign('content',$data['Name'].'--'.$data['Telephone'].'--'.$data['Address']);
        $this->assign('total',$data['Total']);
        $this->display();
    }
    public  function myorder(){
        $consumer=D('Consumer');
        $consumerID=$consumer->fetchID($_SESSION['id']);
        $order=D('order');
        $myorder=$order->where('ConsumerID='.$consumerID)->select();
        $goods=array();
        foreach($myorder as $key =>$vo)
        {
            $arr=explode(';',$vo['goods']);
            foreach($arr as $k =>$varr){
                $myorder[$key]['goods1'][$k]=$varr;
            }
        }
        $this->assign('user',$_SESSION['id']);
        $this->assign('data',$myorder);
        $this->display();
    }
    public function search(){
        $this->assign('user',$_SESSION['id']);
        if($_SESSION['id']) {
            $consumer=D('Consumer');
            $consumerID=$consumer->fetchID($_SESSION['id']);
            $shoppingcart=D('shoppingcart');
            $result=$shoppingcart->where('ConsumerID='.$consumerID)->count();
            $this->assign('count',$result);
        }
        $search_name = '%'.$_POST['search_name'].'%';
        $goods=M('Goods');
        $where['Name']=array('like',$search_name);
        $result=$goods->where($where)->select();
        $this->assign('data',$result);
        $this->display();
    }
}